<?php

/**
 * formule actions.
 *
 * @package    ProjetBar
 * @subpackage formule
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class formuleActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->formules = Doctrine_Core::getTable('formule')
      ->createQuery('a')
      ->execute();
  }

  public function executeShow(sfWebRequest $request)
  {
	$this->idMenu = $request->getParameter('id_menu');
	$this->idCommandeClient = $request->getParameter('id_commande_client');
	$this->menu = Doctrine_Core::getTable('menu')->find(array($request->getParameter('id_menu')));
    $this->categories = Doctrine_Query::Create()
		->select('c.*')
		->from('categorie c, formule f')
		->where('c.id_categorie = f.id_categorie')
		->andWhere('f.id_menu = '.$this->idMenu)
		->execute();
	try{
		$menu = new commandeMenu();
		$menu->id_commande_client = $this->idCommandeClient;
		$menu->id_menu = $this->idMenu;
		$menu->save();
	}catch(Exception $e){}
	$this->idCommandeMenu = $menu->getIdCommandeMenu();
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new formuleForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new formuleForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($formule = Doctrine_Core::getTable('formule')->find(array($request->getParameter('id_categorie'),
                             $request->getParameter('id_menu'))), sprintf('Object formule does not exist (%s).', $request->getParameter('id_categorie'),
                             $request->getParameter('id_menu')));
    $this->form = new formuleForm($formule);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($formule = Doctrine_Core::getTable('formule')->find(array($request->getParameter('id_categorie'),
                             $request->getParameter('id_menu'))), sprintf('Object formule does not exist (%s).', $request->getParameter('id_categorie'),
                             $request->getParameter('id_menu')));
    $this->form = new formuleForm($formule);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($formule = Doctrine_Core::getTable('formule')->find(array($request->getParameter('id_categorie'),
                             $request->getParameter('id_menu'))), sprintf('Object formule does not exist (%s).', $request->getParameter('id_categorie'),
                             $request->getParameter('id_menu')));
    $formule->delete();

    $this->redirect('formule/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $formule = $form->save();

      $this->redirect('formule/edit?id_categorie='.$formule->getIdCategorie().'&id_menu='.$formule->getIdMenu());
    }
  }
}
